﻿using System;
using UnityEngine;

public class RecoverWeaponDurabilityOnCharacterEnter : MonoBehaviour
{
    private Weapon GetWeapon(GameObject user)
    {
        if (user == null)
        {
            return null;
        }
        Inventory component = user.GetComponent<Inventory>();
        return ((component == null) ? null : component.weapon);
    }

    public void Interact(GameObject target)
    {
        Weapon weapon = this.GetWeapon(target);
        if (weapon != null)
        {
            weapon.durability = weapon.maxDurability;
        }
    }

    private void OnTriggerEnter(Collider co)
    {
        if (co.gameObject.layer == GameLayers.Character.index)
        {
            this.Interact(co.gameObject);
        }
    }

    private void Start()
    {
        Collider component = base.GetComponent<Collider>();
        if (component != null)
        {
            if (component is MeshCollider)
            {
                (component as MeshCollider).convex = true;
            }
            component.isTrigger = true;
        }
    }
}

